package com.otaku.crawler.parser;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.otaku.crawler.model.NASaveMoneyPojo;
import com.otaku.crawler.util.EmailHelper;

public class NASaveMoneyParser {
	private static final Logger log = Logger.getRootLogger();
	
	private static final String REPLACEMENT = "点击..>>;";

	private static final String CON_DIV_IDENTIFER = "con_lecon";
	private static final String CON_IMG_IDENTIFIER = "left_img";
	private static final String CON_TEXT_IDENTIFIER = "right_text";
	private static final String CON_TIME_IDENTIFIER = "ri_textdownri";

	private static final String ROW2_DIV_IDENTIFIER = "row2";
	private static final String ROW2_IMG_IDENTIFIER = "overflow: hidden; width: 144px";
	private static final String ROW2_NAME_IDENTIFIER = "margin-bottom:20px";
	private static final String ROW2_TEXT_IDENTIFIER = "dealdesc";

	private static final String URL_HEADER = "http://www.dealmoon.com";

	public static List<NASaveMoneyPojo> parse(Document d) {
		List<NASaveMoneyPojo> rl = new ArrayList<NASaveMoneyPojo>();

		rl.addAll(getCON_List(d));
		rl.addAll(getROW2_List(d));

		return rl;
	}

	private static List<NASaveMoneyPojo> getROW2_List(Document d){
//		System.out.println(d.toString());
		List<NASaveMoneyPojo> rl = new ArrayList<NASaveMoneyPojo>();

		Elements row2l = d.select("div." + ROW2_DIV_IDENTIFIER);

		if (row2l != null && row2l.size() > 0) {
//			System.out.println("have row2 div");
			for (int i = 0; i < row2l.size(); i++) {
				Element itemE = row2l.get(i);
				if(itemE.children().size()==0) continue;
				
				try {
					Element nameE = itemE.select(
							"div[style=" + ROW2_NAME_IDENTIFIER + "]").first();
					String name = nameE.text().trim();
					if(name.startsWith("已过期")) continue;
					
					Element imgE = itemE.select(
							"td[class=bz]").first();
					String imgUrl = imgE.select("img[src]").first().attr("surl");
					String url = URL_HEADER
							+ imgE.select("a[href]").first().attr("href");

					Element infoE = itemE.select("div." + ROW2_TEXT_IDENTIFIER)
							.first();
					String info = infoE.text().trim().replaceAll(REPLACEMENT, "");

					rl.add(new NASaveMoneyPojo(name, info, imgUrl, url, ""));
				} catch (NullPointerException e) {
					log.error("EXCEPTION:"+itemE.toString(), e);
					e.printStackTrace();
					continue;
				}
			}
		}
		return rl;
	}

	/**
	 * deal with div with class name CON_DIV_IDENTIFIER
	 * 
	 * @param d
	 * @return
	 */
	private static List<NASaveMoneyPojo> getCON_List(Document d){
		List<NASaveMoneyPojo> rl = new ArrayList<NASaveMoneyPojo>();

		Elements e = d.select("div." + CON_DIV_IDENTIFER);
		if(e==null || e.size()==0) return rl;
		
		Elements lis = e.first().select("li[id]");
		
		if (lis != null && lis.size()>0) {
//			System.out.println("have row2 div");
			for (int i = 0; i < lis.size(); i++) {
				// li
				Element li = lis.get(i);
				try {
					if(li.children().size()==0) continue;
					
					Elements names = li.select("div[class="+CON_TEXT_IDENTIFIER+"]");
					if(names==null || names.size()==0) continue;
					Element nameE = names.first();
					String name = nameE.select("h2").first().text().trim();
					if(name.startsWith("已过期")) continue;
					
					String info = "";
					Elements infos = li.select("div[class=black]").first().select("li");
					for(int j=0;j<infos.size();j++){
						info += infos.get(j).text().trim()+";";
					}
					info = info.replaceAll(REPLACEMENT, "");
					
					String time = li.select("div[class="+CON_TIME_IDENTIFIER+"]").first().text().trim();
					
					Elements imgs = li.select("div[class="+CON_IMG_IDENTIFIER+"]");
					if(imgs==null || imgs.size()==0) continue;
					Element imgE = li.select("div[class="+CON_IMG_IDENTIFIER+"]").first();
					String url = URL_HEADER+imgE.select("a[href]").first().attr("href");
					String imgUrl = imgE.select("img[src]").first().attr("surl");
					
					rl.add(new NASaveMoneyPojo(name, imgUrl,url, info, time));
				} catch (NullPointerException e1) {
					log.error("EXCEPTION:"+li.toString(), e1);
					e1.printStackTrace();
					continue;
				}
			}
		}
		return rl;
	}
}
